<?php

namespace app\admin\model\prl;

use think\Model;


class Viewsfields extends Model
{
    // 表名
    protected $table = 'prl_viewsfields';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'public_text',
        'common_text',
        'views_ids',//关联视图中间ids
    ];

    protected static function scopePublic($q, $v = 0)
    {
        $q->where('public', $v);
    }
    

    
    public function getPublicList()
    {
        return ['0' => __('Public 0'), '1' => __('Public 1')];
    }     

    public function getCommonList()
    {
        return ['0' => __('Common 0'), '1' => __('Common 1')];
    }     


    public function getPublicTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['public']) ? $data['public'] : '');
        $list = $this->getPublicList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCommonTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['common']) ? $data['common'] : '');
        $list = $this->getCommonList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //视图
    public function views()
    {
        return $this->belongsToMany('Views', 'app\\admin\\model\\prl\\Viewsfieldsaccess');
    }

    public function getViewsIdsAttr($value, $data)
    {
        $ids = [];
        foreach ($this->views as $view) {
            $ids[] = $view->pivot->views_id;
        }
        return $ids;
    }
}
